//
//  tcsf/ccs.h
//  Created by tommywang on 13-4-28.
//

#ifndef _tcsf_ccs_h_
#define _tcsf_ccs_h_

#include <string>
using namespace std;
#include "tdlexcept.h"
#include "net/socket.h"
#ifdef SYS_EPOLL_H
	#include "net/epoll_wrapper.h"
#else
	#include <sys/select.h>
#endif
#include "sys/clock.h"
#include "component/logger.h"
#include "component/config.h"
using namespace tdl;
#include "sof.h"
#include "nreq.h"
#include "btypes.h"
#include "ndchannel.h"
using namespace tcsf;

namespace tcsf {
////////////////
class ccs
{
public:
	typedef tcsf::ndchannel<tdl::udp_socket>			udp_channel;
	typedef std::map<int, udp_channel*>					udp_channel_type;

	typedef tcsf::ndchannel<tdl::passive_tcp_socket>	tcp_channel;
	typedef std::map<int, tcp_channel*>					tcp_channel_type;

protected:
	bool				m_daemon;
	std::string			m_conf_file;

	tdl::file_config	m_conf;

	sys_logger_type		m_sys_logger;
	access_logger_type	m_access_logger;

#ifdef SYS_EPOLL_H
	tdl::epoll			m_epoll;
#endif

	udp_channel_type	m_udp_channels;

	tcp_channel_type	m_tcp_channels;

	tdl::thread_clock	m_clock;

protected:
	void _init_args(int argc, char* argv[]);
	void _init_logger();
	void _init_network();

#ifdef SYS_EPOLL_H
	void __handle_network_epoll__();
#else
	void __handle_network_select__();
#endif

	void __handle_udp__(udp_channel* pcn);

	void __handle_tcp__(tcp_channel* pcn);

	template <typename Tchannel>
	void __buss_thread_main(Tchannel* pcn);

public:
	ccs();
	
	~ccs();
	
	void show_usage();
	
	void initialize(int argc, char* argv[]);
	
	void run();
	
	int terminate(int excode);

};	// class ccs

////////////////////
}	// namespace tcsf
#endif


